import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

@SuppressWarnings("serial")
public class SpeedControl extends JFrame {


   MainPanel mainpanel;
   
	public SpeedControl(MainPanel mainpanel){
		this.mainpanel = mainpanel;
		
		sliderPanel sliderPanel = new sliderPanel();
		sliderPanel.setBorder(new TitledBorder("Speed"));
		
		algorithmPanel algorithm = new algorithmPanel();
		algorithm.setBorder(new TitledBorder("Algorithm"));
		
		add(sliderPanel);
		add(algorithm);
		
		setLayout(new GridLayout(2,1,0,0));
		setSize(300,200);
		setLocationRelativeTo(null);
		setResizable(false);
	}

	public void showWindow(){
		setVisible(true);
	}
	
	class algorithmPanel extends JPanel implements ActionListener , DocumentListener{
		
		JTextField alpha,gamma,epsilon;
		JComboBox combo;
		String algList[];
		
		public algorithmPanel(){
			
		    algList = new String[2];
			setLayout(new BorderLayout());
			algList[0] = "Sarsa";
			algList[1] = "QLearning";
			combo = new JComboBox(algList);
			combo.setSelectedIndex(0);
			combo.addActionListener(this);
			JPanel comboPanel = new JPanel(new BorderLayout());
			comboPanel.add(combo,BorderLayout.NORTH);
			
			JPanel variable = new JPanel(new GridLayout(2,3));
			
			JLabel s1 = new JLabel("Alpha");
			JLabel s2 = new JLabel("Gamma");
			JLabel s3 = new JLabel("Epsilon");
            
			variable.add(s1);
			variable.add(s2);
			variable.add(s3);
            
			alpha = new JTextField("0.5");
			gamma = new JTextField("0.5");
		    epsilon = new JTextField("0.1");
		    
		    alpha.getDocument().addDocumentListener(this);
		    gamma.getDocument().addDocumentListener(this);
		    epsilon.getDocument().addDocumentListener(this);
		    
			variable.add(alpha);
			variable.add(gamma);
			variable.add(epsilon);
			
			add(combo,BorderLayout.NORTH);
			add(variable,BorderLayout.SOUTH);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			JComboBox cb;
			String s ; 
			
			double alpha = Double.parseDouble(this.alpha.getText());
			double gamma = Double.parseDouble(this.gamma.getText());
			double epsilon = Double.parseDouble(this.epsilon.getText());
			
			cb = (JComboBox)(e.getSource());
			s = (String) cb.getSelectedItem();
	
			if(s.equals(algList[0]))
				mainpanel.temporal = new Sarsa(alpha,gamma,epsilon,0);
			else
				mainpanel.temporal = new QLearning(alpha,gamma,epsilon,0);
			
			for(int i = 0;i<3;i++){
				if(mainpanel.threadRacer[i]!=null)
					mainpanel.racer[i].temporal = mainpanel.temporal;
				if(mainpanel.threadPolice[i]!=null)
					mainpanel.police[i].temporal = mainpanel.temporal;
			}
			
		}
		@Override
		public void changedUpdate(DocumentEvent e) {
		}
		@Override
		public void insertUpdate(DocumentEvent e) {
			double alpha = Double.parseDouble(this.alpha.getText());
			double gamma = Double.parseDouble(this.gamma.getText());
			double epsilon = Double.parseDouble(this.epsilon.getText());
			String s = (String)combo.getSelectedItem();
			if(s.equals(algList[0]))
				mainpanel.temporal = new Sarsa(alpha,gamma,epsilon,0);
			else
				mainpanel.temporal = new QLearning(alpha,gamma,epsilon,0);
		
			for(int i = 0;i<3;i++){
				if(mainpanel.threadRacer[i]!=null)
					mainpanel.racer[i].temporal = mainpanel.temporal;
				if(mainpanel.threadPolice[i]!=null)
					mainpanel.police[i].temporal = mainpanel.temporal;
			}
		}
		@Override
		public void removeUpdate(DocumentEvent e) {
			// TODO Auto-generated method stub
			
		}
	}
	
	class sliderPanel extends JPanel implements ChangeListener{
		 JSlider slider  = new JSlider(JSlider.HORIZONTAL,10,100,20);
		
		public sliderPanel(){
		
			setLayout(new BorderLayout());
			slider.addChangeListener(this);
			slider.setPaintLabels(true);
			slider.setPaintTicks(true);
			slider.setMajorTickSpacing(10);
			slider.setMinorTickSpacing(1);
			add(slider,BorderLayout.CENTER);
	
		}

		@Override
		public void stateChanged(ChangeEvent arg0) {
			
			JSlider source = (JSlider)arg0.getSource();
			int change =(int)source.getValue();
			Component.volume=(int) (Component.maxVolume/(Math.pow(	2, change/10)));
		
		}
	}
}